import datetime
import time
import random
from django.shortcuts import render
import json
from random import randrange

from django.http import HttpResponse
from rest_framework.views import APIView

from pyecharts.charts import Bar
from pyecharts.charts import Line
from pyecharts import options as opts
from .models import *
from django.db.models import Q


def response_as_json(data):
    json_str = json.dumps(data)
    response = HttpResponse(
        json_str,
        content_type="application/json",
    )
    response["Access-Control-Allow-Origin"] = "*"
    return response


def json_response(data, code=200):
    data = {
        "code": code,
        "msg": "success",
        "data": data,
    }
    return response_as_json(data)


def json_error(error_string="error", code=500, **kwargs):
    data = {
        "code": code,
        "msg": error_string,
        "data": {}
    }
    data.update(kwargs)
    return response_as_json(data)


JsonResponse = json_response
JsonError = json_error


def table_sales_rank(request):
    if request.method == 'POST':
        month_in = request.POST.get('month_inp')
        s = SalesPerformance.objects.filter(performance_month=month_in).order_by('performance_rank')
        x_lis = [i.eid.eid for i in s]
        y_lis = [i.sales_performance for i in s]
        si = SalesIndex.objects.filter(index_month=month_in)
        for i in si:
            i.count = (i.sales_num / i.goods.gprice)
        gx_lis = [i.goods.goods_id for i in si]
        gy1_lis = [i.profit_rate for i in si]
        gy2_lis = [i.kpi_rate for i in si]
        gy3_lis = [i.sales_num for i in si]
        gy4_lis = [i.return_rate for i in si]
        gyc_lis = [i.count for i in si]
        x_type = list(set([i.goods.goods_type for i in si]))
        gy10_lis = [0 for i in range(len(x_type))]
        gy20_lis = [0 for i in range(len(x_type))]
        gy30_lis = [0 for i in range(len(x_type))]
        gy40_lis = [0 for i in range(len(x_type))]
        gyc0_lis = [0 for i in range(len(x_type))]
        dic1 = []
        dic2 = []
        dic3 = []
        dic4 = []
        dic5 = []
        dic0 = []
        c1 = 0
        c2 = 0
        c3 = 0
        c4 = 0
        c5 = 0
        for i in si:
            if i.goods.goods_type == x_type[0]:
                c1 += 1
                gy10_lis[0] += i.profit_rate
                gy20_lis[0] += i.kpi_rate
                gy30_lis[0] += i.sales_num
                gy40_lis[0] += i.return_rate
                gyc0_lis[0] += i.count
                dic1.append({'name': i.goods.goods_id, 'value': i.count})
            elif i.goods.goods_type == x_type[1]:
                c2 += 1
                gy10_lis[1] += i.profit_rate
                gy20_lis[1] += i.kpi_rate
                gy30_lis[1] += i.sales_num
                gy40_lis[1] += i.return_rate
                gyc0_lis[1] += i.count
                dic2.append({'name': i.goods.goods_id, 'value': i.count})

            elif i.goods.goods_type == x_type[2]:
                c3 += 1
                gy10_lis[2] += i.profit_rate
                gy20_lis[2] += i.kpi_rate
                gy30_lis[2] += i.sales_num
                gy40_lis[2] += i.return_rate
                gyc0_lis[2] += i.count
                dic3.append({'name': i.goods.goods_id, 'value': i.count})
            elif i.goods.goods_type == x_type[3]:
                c4 += 1
                gy10_lis[3] += i.profit_rate
                gy20_lis[3] += i.kpi_rate
                gy30_lis[3] += i.sales_num
                gy40_lis[3] += i.return_rate
                gyc0_lis[3] += i.count
                dic4.append({'name': i.goods.goods_id, 'value': i.count})
            elif i.goods.goods_type == x_type[4]:
                c5 += 1
                gy10_lis[4] += i.profit_rate
                gy20_lis[4] += i.kpi_rate
                gy30_lis[4] += i.sales_num
                gy40_lis[4] += i.return_rate
                gyc0_lis[4] += i.count
                dic5.append({'name': i.goods.goods_id, 'value': i.count})
        if c1 == 0:
            c1 = 1
        if c2 == 0:
            c2 = 1
        if c3 == 0:
            c3 = 1
        if c4 == 0:
            c4 = 1
        if c5 == 0:
            c5 = 1
        dic0.append({'name': x_type[0], 'value': gyc0_lis[0]})
        dic0.append({'name': x_type[1], 'value': gyc0_lis[1]})
        dic0.append({'name': x_type[2], 'value': gyc0_lis[2]})
        dic0.append({'name': x_type[3], 'value': gyc0_lis[3]})
        dic0.append({'name': x_type[4], 'value': gyc0_lis[4]})
        gy10_lis[0] = gy10_lis[0] / c1
        gy10_lis[1] = gy10_lis[1] / c2
        gy10_lis[2] = gy10_lis[2] / c3
        gy10_lis[3] = gy10_lis[3] / c4
        gy10_lis[4] = gy10_lis[4] / c5

        gy20_lis[0] = gy10_lis[0] / c1
        gy20_lis[1] = gy10_lis[1] / c2
        gy20_lis[2] = gy10_lis[2] / c3
        gy20_lis[3] = gy10_lis[3] / c4
        gy20_lis[4] = gy10_lis[4] / c5

        gy40_lis[0] = gy10_lis[0] / c1
        gy40_lis[1] = gy10_lis[1] / c2
        gy40_lis[2] = gy10_lis[2] / c3
        gy40_lis[3] = gy10_lis[3] / c4
        gy40_lis[4] = gy10_lis[4] / c5

        return render(request, '../templates/index.html',
                      {'s': s, 'x': x_lis, 'y': y_lis, 'si': si, 'gx_lis': gx_lis, 'gy1_lis': gy1_lis,
                       'gy2_lis': gy2_lis, 'gy3_lis': gy3_lis, 'gy4_lis': gy4_lis, 'gyc_lis': gyc_lis, 'x_type': x_type,
                       'gy10_lis': gy10_lis, 'gy20_lis': gy20_lis, 'gy30_lis': gy30_lis, 'gy40_lis': gy40_lis,
                       'gyc0_lis': gyc0_lis
                          , 'dic1': dic1, 'dic2': dic2, 'dic3': dic3, 'dic4': dic4, 'dic5': dic5, 'dic0': dic0})
def zb_yj(request):
    ak=LiveRecord.objects.all()
    zb=list(set([i.eid.eid.eid for i in ak]))
    zbif=ZbInfo.objects.all()
    od=OrderInfo.objects.all()
    zblive=[]
    zbname=[]
    for i in zb:
        lis=[]
        for j in ak:
            if j.eid.eid.eid == i:
                lis.append(j.live_record_id)
        zblive.append(lis)
        for k in zbif:
            if k.eid.eid == i:
                zbname.append(k.zbname)
    zbsum=[]
    print(zbname)
    print(zblive)
    zb_sum=[]
    for i in zblive:
        sum=0
        for j in i:
            for k in od:
                if k.live_record_id == j :
                    sum+=k.sum_price
        zb_sum.append(sum)
    print(zb_sum)
    diclis=[]
    arr=['抖音','斗鱼','B站']
    for i in range(len(zb)):
        dic={}
        dic["id"]=zb[i]
        dic['name']=zbname[i]
        dic['sum']=zb_sum[i]
        dic['rate']=random.random()*0.5
        dic['plat']=random.choice(arr)
        diclis.append(dic)
    return render(request,'../templates/index.html', {'dic':diclis})
